package com.onepiece.shipelves.common.utils;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author wangxj
 * @Desc: 关于数字的util
 * @date 2019-09-25
 */
public class NumberUtil {

    public static double getFormatNum(double dividend, double divisor, int digit) {
        if (divisor == 0d) {
            return 0d;
        }
        String s = String.valueOf(dividend / divisor);
        return new BigDecimal(s).setScale(digit, RoundingMode.UP).doubleValue();
    }

    /**
     * 计算标准差
     *
     * @param list
     * @return
     */
    public static double getTrackingError(List<Double> list) {
        if (!CollectionUtils.isEmpty(list)) {
            double sum = 0;
            for (double s : list) {
                sum += s;
            }
            double avg = sum / list.size();
            double total = 0;
            for (double s : list) {
                total += (s - avg) * (s - avg);
            }
            return Math.sqrt(total / list.size());
        }
        return 0;
    }
}
